package cn.yiyuan;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @user m
 * @project Car_mall_system
 * @date 2024-01-09
 * @package cn.yiyuan
 */
public class ChatWeb {



        public static void main(String[] args) {
            // Replace "YOUR_API_KEY" with your actual API key
            String apiKey = "";   //api秘钥

            // Construct the API URL
            String apiUrl = "http://apis.juhe.cn/fapigx/pyqwenan/query?key=" + apiKey;

            try {
                // Create a URL object
                URL url = new URL(apiUrl);

                // Open a connection to the URL
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                // Set the request method to GET
                connection.setRequestMethod("GET");

                // Get the response code
                int responseCode = connection.getResponseCode();

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    // Read the response
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder response = new StringBuilder();
                    String line;

                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }

                    reader.close();

                    // Parse the JSON response
                    // You can use a JSON library like Jackson or Gson for better handling
                    System.out.println("Response: " + response.toString());
                } else {
                    System.out.println("Error: HTTP request failed with code " + responseCode);
                }

                // Close the connection
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


